package com.freedy.concurrent.javaThread.join;

import lombok.extern.log4j.Log4j;

import java.util.concurrent.TimeUnit;

/**
 * 有时效的 join
 * 没等够时间
 * @author Freedy
 * @date 2021/4/20 18:09
 */
@Log4j
public class Test4 {
    static int r1 = 0;
    static int r2 = 0;
    public static void main(String[] args) throws InterruptedException {
        test3();
    }
    public static void test3() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            sleep(2);
            r1 = 10;
        });
        long start = System.currentTimeMillis();
        t1.start();
        // 线程执行结束会导致 join 结束
        t1.join(1500);
        long end = System.currentTimeMillis();
        log.debug("r1: "+r1+" r2: "+r2+" cost: "+(end - start)+"");
    }
    private static void sleep(int i) {
        try {
            TimeUnit.SECONDS.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
